package com.qlogic.commons.utils.reflect;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;

public class PackageDescriptor implements Serializable, Comparable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3192364908543358453L;
	
	private String name;
	
	private Hashtable sources = new Hashtable ();
	
	private ArrayList sourcesList = new ArrayList ();
	
	private ClassDecorator decorator;
	
	public PackageDescriptor () {
	}
	
	public PackageDescriptor (String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	public void addSource (Class clazz) {
		DecoratedSource decoratedSource = decorator.decorate (clazz, this);
		if (decoratedSource == null) {
			return;
		}
		sourcesList.add (decoratedSource);
		sources.put (decoratedSource.getName (), decoratedSource);
	}
	
	public DecoratedSource getSource (String name) {
		return (DecoratedSource)sources.get (name);
	}
	
	public DecoratedSource getSource (int index) {
		return (DecoratedSource)sourcesList.get (index);
	}
	
	public Enumeration getSourceNames () {
		return sources.keys ();
	}
	
	public int getSourcesCount () {
		return sourcesList.size ();
	}
	
	void sortSources () {
		Collections.sort (sourcesList, decorator);
	}

	public int compareTo(Object anotherPackageDescriptor) {
		return this.getName ().compareTo (((PackageDescriptor)anotherPackageDescriptor).getName ());
	}

	public ClassDecorator getDecorator() {
		return decorator;
	}

	public void setDecorator(ClassDecorator decorator) {
		this.decorator = decorator;
	}
	
}
